package com.enhanced.mqtt;

import com.enhanced.mqtt.client.*;

import java.util.Scanner;

/**
 * EMQX MQTT演示程序主入口
 * 提供多种MQTT功能特性的演示选项
 */
public class EmqxMqttDemo {
    
    private static Scanner scanner = new Scanner(System.in);
    
    public static void main(String[] args) {
        System.out.println("========================================");
        System.out.println("      EMQX MQTT 增强功能演示程序");
        System.out.println("========================================");
        System.out.println("本程序演示如何使用EMQX的各种高级功能");
        System.out.println("确保EMQX服务器已启动：");
        System.out.println("docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:latest");
        System.out.println();
        
        boolean exit = false;
        
        while (!exit) {
            try {
                System.out.println("请选择要演示的功能：");
                System.out.println("1. 基础MQTT客户端示例");
                System.out.println("2. 高级MQTT功能演示");
                System.out.println("3. SSL/TLS加密连接示例");
                System.out.println("4. 多协议连接示例（TCP/WebSocket）");
                System.out.println("5. MQTT 5.0特性演示");
                System.out.println("0. 退出");
                
                System.out.print("请输入选项(0-5): ");
                int choice = getIntegerInput();
                
                try {
                    switch (choice) {
                        case 1:
                            runBasicMqttClient();
                            break;
                        case 2:
                            runAdvancedMqttClient();
                            break;
                        case 3:
                            runSSLMqttClient();
                            break;
                        case 4:
                            runMultiProtocolClient();
                            break;
                        case 5:
                            runMqtt5Client();
                            break;
                        case 0:
                            exit = true;
                            System.out.println("感谢使用EMQX MQTT演示程序！");
                            break;
                        default:
                            System.out.println("无效的选择！");
                    }
                } catch (Exception e) {
                    System.out.println("演示过程中发生错误: " + e.getMessage());
                    e.printStackTrace();
                }
                
                if (!exit) {
                    System.out.println("\n按回车键继续...");
                    scanner.nextLine();
                    System.out.println("\n========================================\n");
                }
            } catch (Exception e) {
                System.out.println("输入处理异常: " + e.getMessage());
                scanner.nextLine(); // 清除输入缓冲区
            }
        }
        
        // 只在程序最终退出时关闭Scanner
        scanner.close();
    }
    
    /**
     * 安全地获取整数输入
     */
    private static int getIntegerInput() {
        while (true) {
            try {
                int input = scanner.nextInt();
                scanner.nextLine(); // 消费换行符
                return input;
            } catch (Exception e) {
                System.out.println("请输入有效的数字。");
                scanner.nextLine(); // 清除错误输入
                System.out.print("请重新输入: ");
            }
        }
    }
    
    /**
     * 运行基础MQTT客户端示例
     */
    private static void runBasicMqttClient() throws Exception {
        System.out.println("\n===== 基础MQTT客户端示例 =====");
        // 调用客户端示例，传递Scanner
        BasicMqttClient.runDemo(scanner);
    }
    
    /**
     * 运行高级MQTT功能演示
     */
    private static void runAdvancedMqttClient() throws Exception {
        System.out.println("\n===== 高级MQTT功能演示 =====");
        // 调用客户端示例，传递Scanner
        AdvancedMqttClient.runDemo(scanner);
    }
    
    /**
     * 运行SSL/TLS加密连接示例
     */
    private static void runSSLMqttClient() throws Exception {
        System.out.println("\n===== SSL/TLS加密连接示例 =====");
        // 调用客户端示例，传递Scanner
        SSLMqttClient.runDemo(scanner);
    }
    
    /**
     * 运行多协议连接示例
     */
    private static void runMultiProtocolClient() throws Exception {
        System.out.println("\n===== 多协议连接示例 =====");
        // 调用客户端示例，传递Scanner
        MultiProtocolClient.runDemo(scanner);
    }
    
    /**
     * 运行MQTT 5.0特性演示
     */
    private static void runMqtt5Client() throws Exception {
        System.out.println("\n===== MQTT 5.0特性演示 =====");
        System.out.println("注意：MQTT 5.0演示需要额外的依赖库，请确保已添加Paho MQTT v5客户端依赖");
        System.out.print("是否继续？(y/n): ");
        String confirm = scanner.nextLine().trim().toLowerCase();
        
        if ("y".equals(confirm)) {
            try {
                // 调用客户端示例，传递Scanner
                Mqtt5Client.runDemo(scanner);
            } catch (NoClassDefFoundError e) {
                System.out.println("错误：缺少MQTT 5.0客户端依赖");
                System.out.println("请在pom.xml中添加以下依赖：");
                System.out.println("<dependency>");
                System.out.println("    <groupId>org.eclipse.paho</groupId>");
                System.out.println("    <artifactId>org.eclipse.paho.mqttv5.client</artifactId>");
                System.out.println("    <version>1.2.5</version>");
                System.out.println("</dependency>");
            }
        }
    }
} 